package com.fate.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fate.domain.Order;
import com.fate.service.OrderService;
import com.fate.vo.OrderConfirm;
import com.fate.vo.OrderResult;
import com.fate.vo.OrderStatus;
import com.netflix.discovery.converters.Auto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("p/myOrder")
@Api(tags = "订单接口管理")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("orderCount")
    @ApiOperation("获取用户的订单状态")
    public ResponseEntity<OrderStatus>getOrderCount(){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        OrderStatus orderStatus = orderService.getOrderCount(userId);
        return ResponseEntity.ok(orderStatus);

    }

    @GetMapping("myOrder")
    @ApiOperation("分页查询用户订单详情")
    public ResponseEntity<Page<Order>>getOrderPage(Page<Order>page,Integer status){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        Page<Order> orderPage = orderService.getOrderPage(page, status, userId);
        return ResponseEntity.ok(orderPage);
    }

    //订单确认的接口
    //1.入参 1.1从购物车过来 带购物车ids集合
    // 1.2 从商品进来 带什么进来? prodId skuId count
    //2 出参 对象 包含 用户的默认收货地址 商品集合 商城公用优惠券 留言 订单总金额
    @PostMapping("confirm")
    @ApiOperation("订单确认接口")
    public ResponseEntity<OrderResult> confirmOrder(@RequestBody OrderConfirm orderConfirm){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
            OrderResult orderResult = orderService.orderConfirm(userId,orderConfirm);
            return ResponseEntity.ok(orderResult);
    }

    /**
     * 入参 订单确认返回对象
     * 出参 订单号
     * 前台拿到订单号
     * 发一个请求 根据订单生成支付的预订单
     */
    @PostMapping("submit")
    @ApiOperation("订单提交")
    public ResponseEntity<String> submitOrder(@RequestBody OrderResult orderResult){
        String userId = SecurityContextHolder.getContext().getAuthentication().toString();
       String orderNum = orderService.submit(userId,orderResult);
       return ResponseEntity.ok(orderNum);
    }

    /**
     *根据订单号查询订单支付状态
     */
    @GetMapping("query")
    public ResponseEntity<Boolean> queryOrderStatus(@RequestParam String orderSn){
        int count = orderService.count(new LambdaQueryWrapper<Order>()
                .eq(Order::getOrderNumber, orderSn)
                .eq(Order::getStatus, 2)
        );
        return ResponseEntity.ok(count>0);
    }

    /**
     * 修改订单状态 已经支付
     */
    @PostMapping("changeOrderStatus")
    void changeOrderStatus(@RequestBody String orderNum){
        orderService.changeOrderStatus(orderNum);
    }
}
